<template>
  <div class="container">
    <el-dialog v-model="state.dialog.isShowDialog" :title="state.dialog.titleObj[state.dialog.type]"
      label-position="top" width="800px">
      <el-form ref="dialogFormRef" :model="state.ruleForm" :rules="state.ruleRules" label-width="auto">
        <el-form-item label="标签名称:" prop="name">
          <el-input v-model="state.ruleForm.name" placeholder="请输入" clearable :disabled="isDisabledFn" />
        </el-form-item>
         <el-form-item label="英文标签名称:" prop="abroad_name">
          <el-input v-model="state.ruleForm.abroad_name" placeholder="请输入" clearable :disabled="isDisabledFn" />
        </el-form-item>
        <el-form-item label="追加工期:" prop="days_time">
          <el-input v-model.number="state.ruleForm.days_time" placeholder="请输入" clearable :disabled="isDisabledFn" />
        </el-form-item>
        <el-form-item label="是否默认勾选:" prop="required">
          <el-radio-group v-model="state.ruleForm.required">
            <el-radio v-for="(item, index) in state.requiredArr" :key="index" :label="item.value">
              {{ item.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template v-if="!isDisabledFn" #footer>
        <span class="dialog-footer">
          <el-button @click="onCancel" size="default">取 消</el-button>
          <el-button type="primary" size="default" @click="onSubmit">保 存</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="addLeadTag">
import { reactive, onMounted, computed } from 'vue';
import { addedservicesAdd, addedservicesEdit } from '/@/api/startfab/index';
import { clueTypeArr, requiredArr, accountStatusArr, navArr } from '/@/commmon/variate';
import { ElMessage } from 'element-plus';

const emit = defineEmits(['updateList']);
const state = reactive({
  clueTypeArr: clueTypeArr,
  clueStatusArr: navArr.slice(1),
  accountStatusArr: accountStatusArr,
  requiredArr: requiredArr,
  ruleForm: {
    name: '',
    abroad_name: '',
    days_time: '',
    required: 1,
  },

  ruleRules: {
    days_time: [
      {
        required: false,
        message: '追加工期只能填写数字',
        trigger: 'blur'
      },
      {
        // 去掉type: 'number'，改用自定义校验判断是否为有效数字
        validator: (rule, value, callback) => {
          const num = Number(value);
          if (isNaN(num)) {  // 判断是否为有效数字
            callback(new Error('请输入有效的数字'));
          } else if (num <= 0) {
            callback(new Error('追加工期必须大于0'));
          } else if (!Number.isInteger(num)) {
            callback(new Error('追加工期必须是整数'));
          } else {
            callback();
          }
        },
        trigger: 'blur'
      }
    ],
    abroad_name: [
      { required: true, message: '英文名称必填', trigger: 'blur' }
    ],
  },
  dialog: {
    isShowDialog: false,
    row: null,
    type: '',
    titleObj: {
      'view': '查看',
      'edit': '编辑',
      'add': '新增'
    },
    isDisabled: false
  },
  loading: false,
  originalFiles: {},
})
const onSubmit = async () => {
  const params = {
    ...state.ruleForm,
  }
  console.log(params, "params");

  if (state.dialog.type === 'add') {
    addedservicesAdd(params).then((res) => {
      if (res.code === 10000) {
        onCancel()
        emit('updateList')
        ElMessage.success(res.msg);
      }
    })
  } else {
    addedservicesEdit(params).then((res) => {
      if (res.code === 10000) {
        onCancel()
        emit('updateList')
        ElMessage.success(res.msg);
      }
    })
  }
}

const openDialog = (type, row) => {
  const typeArr = ['edit', 'view'];
  state.dialog.row = row;
  state.dialog.type = type;
  closeDialog();
  if (row && typeArr.includes(type)) {
    state.ruleForm = {
      ...state.ruleForm,
      id: row.id,
      name: row.name,
      abroad_name: row.abroad_name,
      days_time: row.days_time,
      required: row.required,
    }
  }
  state.dialog.isShowDialog = true;
};

const closeDialog = () => {
  state.ruleForm = {
    name: '',
    abroad_name: '',
    days_time: '',
    required: 1,
  }
};

const onCancel = () => {
  closeDialog();
  state.dialog.isShowDialog = false;
};

onMounted(() => { })
const isDisabledFn = computed(() => {
  return state.dialog.type === 'view'
})
defineExpose({
  openDialog,
})
</script>
<style scoped lang="scss">
.dialog-footer {
  display: flex;
  align-items: center;
  justify-content: center;
}

:deep(.el-form-item__content) {
  display: block;
}
</style>
